<?php

function CDefault(&$var, $value) {
	if (is_null($var)) $var = $value;
}

function CBox($val) {
	if (is_object($val)) return $val;
	return new CBoxedValue($val);
}

function CUnbox($val) {
	if (is_object($val) && $val instanceof ICUnboxable)
		return $val->unboxedValue();
	else
		return $val;
}

function CSuffixIfPrefixIs($s, $prefix) {
	if (substr($s, 0, strlen($prefix)) == $prefix)
		return substr($s, strlen($prefix));
	else
		return false;
}

function CNameOfMethodWithClassOfObject($target, $prefix, $object) {
	$class = get_class($object);
	do {
		if (method_exists($target, $m = ($prefix . $class)))
			return $m;
		$class = get_parent_class($class);
	} while ($class != null);
	
	return null;
}

function CNew($class) {
	$x = new ReflectionClass($class);
	if ($x->getConstructor()) {
		$f = func_get_args(); array_splice($f, 0, 1);
		return $x->newInstanceArgs($f);
	} else {
		return $x->newInstance();
	}
}

function CPath() {
	$a = func_get_args();
	if (sizeof($a) > 0 && is_array($a[0]))
		$a = $a[0];
	$l = sizeof($a);
	
	if ($l == 0)
		return '.';
	else
		return implode(DIRECTORY_SEPARATOR, $a);
}


?>